﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
namespace WPFExcelReport
{
    public partial class MKS03
    { 
        public MKS03()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VKSTSTELL();
            this.DataContext = MV;             
            InitializeComponent();
        } 
 
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
           
            TBMain.Clear();
            dwData = TMV.TBMain.NewRow(); 
            dwData["KSTMANDANT"] = app.Mandant;           
            SResult = TMV.QKS02(dwData);
            MergeData(SResult, false); 
            base.QueryCommand(sender, e);
        }

        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            
            if (!IsNew)
            {
                IsNew = true; 
                KRS = "";
                LFD = "";
                return;
            }
            
            if (LFD.Trim().Length < 2)
            {
                MessageBox.Show("必须输入编号");
                return;
            }
            dwData = TBMain.NewRow();
            TMV.SetUpNewRow(ref dwData);
            dwData["KSTMANDANT"] = app.Mandant;  
            dwData["KSTKSTKRS"] = KRS.PadRight(2);
            dwData["KSTKST"] = LFD.PadRight(4);            
            TMV.Insert(dwData);
            vwMaster.View.MoveCurrentToFirst();
            IsNew = false;
            KRS = "";
            LFD = "";
            base.NewCommand(sender, e);
        }

        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            PM["KRS"] = dwMaster["KSTKSTKRS"].ToString();
            PM["LFD"] = dwMaster["KSTKST"].ToString();
            TMV.DELKS(PM);
            filter = "KSTKSTKRS='" + dwMaster["KSTKSTKRS"].ToString() + "' AND KSTKST='" + dwMaster["KSTKST"].ToString() + "'";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach(DataRow row in rows)
            {

                row.Delete();
            }
            TBMain.AcceptChanges();
            base.DeleteCommand(sender, e);
        }

        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            base.SaveCommand(sender, e);           
        } 

        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            /*

            DataSet Set = new DataSet();
            cDR.DocType = PRTYPE;
            Set = INTEXBL.QueryVKK(app.Mandant, dwEKK["VKKAUFKRS"].ToString(), dwEKK["VKKAUFLFD"].ToString());
            dwData = VSDDAT.SD22.TBMain.NewRow();
            dwData["SDSA"] = "0013";
            Set.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
            dwData["SDSA"] = "0114";
            Set.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
            dwData["SDSA"] = "0208";
            Set.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
            dwData["SDSA"] = "0734";
            Set.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));

            dwData["SDSA"] = "0241";
            Set.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));

            dwData["SDSA"] = "0477";
            Set.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
            Set.Merge(INTEXDBC.QueryHRSTAMM(dwData, 3));
            cDR.PRINT(Set);*/
        }

   
        public override void LOADBYAUF()
        {
            if (!IsSaved)
                return;
            if (ORDERNUM != KEY)
                return;
            if (KEY.Length == 0)
                return; 
            dwData = TMV.TBMain.NewRow();          
            dwData["KSTMANDANT"] = app.Mandant; 
            dwData["KSTKSTKRS"] = "AND KSTKSTKRS= '" + KEY.Substring(0, 2) + "'";
            dwData["KSTKST"] = "AND KSTKST= '" + KEY.Substring(2, 4) + "'";        
            SResult = TMV.QKS02(dwData);
            TBTemp = TBMain.Clone();
            TBTemp.Merge(SResult.Tables[0]);
            foreach (DataRow row in TBTemp.Rows)
            {
                dwMaster.ItemArray = row.ItemArray;
            }
            base.LOADBYAUF();
        }
    }
}
